package gw.com.android.ui.chart;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import gw.com.android.app.AppMain;
import gw.com.android.presenter.PropertyPresenter;
import gw.com.android.ui.BaseActivity;
import gw.com.android.utils.ColorThemeUtil;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;
import www.com.library.view.TintImageView;

/**
 * Created by reeta.zou on 2016/10/24.
 * 合约属性
 */
public class PropertyActivity extends BaseActivity {

    @BindView(R.id.contract_title)
    TextView mProView;
    @BindView(R.id.list_view)
    RecyclerView mListView;

    private int mUiCode;
    private int mZone;
    private DataItemResult mListData;
    private DataAdapter mAdapter;
    private PropertyPresenter mPresenter;

    @Override
    protected int getLayoutView() {
        return R.layout.activity_chart_property;
    }

    @Override
    protected void initLayoutView() {
        ButterKnife.bind(this);
        mTitleBar.setAppTitle(R.string.property_title);
//        mTitleBar.setLeftResource(R.string.main_menu_item_chart);
        //mTitleBar.setLeftResource("");
        // 设置布局管理器
        mListView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initViewData() {
        mUiCode = getIntent().getIntExtra("uiCode", 0);
        mZone = getIntent().getIntExtra("zone", GTSConst.ZONE_TYPE_FX);
        mPresenter = new PropertyPresenter(mUiCode, mZone);
        mListData = mPresenter.getPropertyData();
        mProView.setText(getString(R.string.property_proname_title, getIntent().getStringExtra("name"), getIntent().getStringExtra("subName")));
        mAdapter = new DataAdapter(this);
        mListView.setAdapter(mAdapter);
    }

    /**
     * 列表适配器
     *
     * @author reeta
     */
    public class DataAdapter extends RecyclerView.Adapter {
        private LayoutInflater mInflater;

        public DataAdapter(Activity act) {
            mInflater = LayoutInflater.from(act);
        }

        public DataItemDetail getItem(int position) {
            if (position < 0 || position >= mListData.getDataCount())
                return null;
            return mListData.getItem(position);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = mInflater.inflate(R.layout.list_item_contract_property, parent, false);
            return new ItemView(itemView);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            final ItemView viewHold = (ItemView) holder;
            if (position % 2 == 1 || getItemCount() - 1 == position) {
                viewHold.itemView.setBackgroundResource(R.color.color_e);
            } else {
                viewHold.itemView.setBackgroundResource(R.color.color_F8F8F8);
            }

            final DataItemDetail item = getItem(position);
            if (null != item) {
                if (getItemCount() - 1 == position) {
                    viewHold.mView.setTextColor(ContextCompat.getColor(PropertyActivity.this, R.color.color_v));
                    viewHold.mView.setPadding(0, DeviceUtil.instance().dip2px(10, getApplicationContext()), 0, DeviceUtil.instance().dip2px(30, getApplicationContext()));
                } else {
                    viewHold.mView.setTextColor(ContextCompat.getColor(PropertyActivity.this, R.color.color_888888));
                    viewHold.mView.setPadding(0, 0, 0, 0);
                }
                viewHold.mView.setText(item.getString("title"));
                viewHold.mValueView.setText(item.getString("value"));
                viewHold.mImageView.setImageResource(0);
                if (mPresenter.isMargin && mPresenter.isMore) {
                    if (item.getString("title").equals(AppMain.getAppString(R.string.property_margin_over_city))
                            || item.getString("title").equals(AppMain.getAppString(R.string.property_margin_over_holiday))) {
                        viewHold.mTextView2.setText(item.getString(GTSConst.JSON_KEY_PROPERTY_LOTS));
                        if (item.getString("title").equals(AppMain.getAppString(R.string.property_margin_over_city))) {
                            viewHold.mValueView2.setText(item.getString(GTSConst.JSON_KEY_PROPERTY_MARGIN));
                            setMarginLayout(GTSConst.JSON_KEY_PROPERTY_UP_MARGIN, item, viewHold);
                        } else {
                            viewHold.mValueView2.setText(item.getString(GTSConst.JSON_KEY_PROPERTY_HOLIDAYMARGIN));
                            setMarginLayout(GTSConst.JSON_KEY_PROPERTY_UP_HOLIDAYMARGIN, item, viewHold);
                        }
                        viewHold.mValueView.setTextColor(ColorThemeUtil.instance().color_k);
                        viewHold.mImageView.setVisibility(View.VISIBLE);
                    } else {
                        viewHold.mValueView.setTextColor(ContextCompat.getColor(PropertyActivity.this, R.color.color_424242));
                        viewHold.mImageView.setImageResource(0);
                        viewHold.mImageView.setVisibility(View.GONE);
                        viewHold.mLayout2.setVisibility(View.GONE);
                    }
                } else {
                    viewHold.mImageView.setImageResource(0);
                    viewHold.mImageView.setVisibility(View.GONE);
                    viewHold.mLayout2.setVisibility(View.GONE);
                }
            }

        }

        /**
         * 设置保证金下拉布局的一些属性值
         *
         * @param key
         * @param item
         * @param viewHold
         */
        private void setMarginLayout(final String key, final DataItemDetail item, final ItemView viewHold) {
            if (item.getBoolean(key)) {
                viewHold.mImageView.setImageResource(R.mipmap.a_small_developup);
                viewHold.mLayout2.setVisibility(View.VISIBLE);
            } else {
                viewHold.mImageView.setImageResource(R.mipmap.a_small_developdown);
                viewHold.mLayout2.setVisibility(View.GONE);
            }
            viewHold.mLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (CommonUtils.isFastDoubleClick()) {
                        return;
                    }
                    if (item.getBoolean(key)) {
                        item.setBooleanValue(key, false);
                        viewHold.mImageView.setImageResource(R.mipmap.a_small_developdown);
                        viewHold.mLayout2.setVisibility(View.GONE);
                    } else {
                        item.setBooleanValue(key, true);
                        viewHold.mImageView.setImageResource(R.mipmap.a_small_developup);
                        viewHold.mLayout2.setVisibility(View.VISIBLE);
                    }
                }
            });
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            if (null != mListData) {
                return mListData.getDataCount();
            }
            return 0;
        }

        class ItemView extends RecyclerView.ViewHolder {

            @BindView(R.id.title_view)
            TextView mView;
            @BindView(R.id.value_view)
            TextView mValueView;
            @BindView(R.id.image_view)
            TintImageView mImageView;
            @BindView(R.id.content_view)
            View mLayout;
            @BindView(R.id.content_view2)
            View mLayout2;
            @BindView(R.id.title_second_view)
            TextView mTextView2;
            @BindView(R.id.value_second_view)
            TextView mValueView2;

            public ItemView(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

}